<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of UserValidator
 *
 * @author Administrator
 */

namespace frontend\library\validator;

use frontend\exception\UserException;

class UserValidator {
    //put your code here

    /**
     * 检测用户名的合法性
     *
     * @param string $username
     * @return boolean|PwError
     */
    public static function isUsernameHasIllegalChar($username) {

        //匹配用户名只能含有中文、数字、大小写字母、'.'、_
        if (0 >= preg_match('/^[\x7f-\xff\dA-Za-z\.\_]+$/', $username)) {
            return true;
        }
        return false;
    }

    public static function isAreaValid($parent, $child) {
        Yii::import('application.service.area.AreaDs');
        $ds = new AreaDs();
        $r = $ds->getAreaById($child);
        if ($r && $r->pid == $parent) {
            return true;
        } else {
            return false;
        }
    }

    /*
     * 验证性别
     */

    public static function isGenderValid($v) {
        return isset(MtEnum::$gender[$v]) ? true : false;
    }

    /*
     * 验证绑定类型
     */

    public static function isBindValid($v) {
        return isset(MtEnum::$bind[$v]) ? true : false;
    }

    public static function isMobileValid($mobile, $uid = 0) {
       
        if (false == ($r = MtValidator::isTelNumber($mobile))) {
            return UserException::E_MOBILE_FORMAT_ERROR;
        }

        if (false !== ($r = self::checkMobileExist($mobile, $uid))) {

            return UserException::E_MOBILE_REPEAT_ERROR;
        }
        return true;
    }

    /**
     * 验证用户名
     *
     * @param string $username  验证的用户名
     * @param int $uid			排除的用户ID
     * @return PwError|boolean
     */
    public static function isUsernameValid($username, $uid = 0) {
      

        if (false !== ($r = self::isUsernameHasIllegalChar($username))) {
            return UserException::E_USERNAME_FORMAT_ERROR;
        }
        if (false === ($r = MtValidator::isLegalLength($username, 1))) {
            return UserException::E_USERNAME_TOOSHORT_ERROR;
        }



        if (true === ($r = MtValidator::isLegalLength($username, 15))) {
            return UserException::E_USERNAME_TOOLONG_ERROR;
        }
//        if (false !== ($r = self::checkUsernameExist($username, $uid))) {
//
//            return new BaseError(BaseError::E_USERNAME_REPEAT);
//        }


        return true;
    }

    public static function isBirthdayValid($birthday) {
        if (0 >= preg_match('/^(19\d{2}|20\d{2})-?(1[0-2]|0?[1-9])-?(0?[1-9]|[1-2][0-9]|3[0-1])$/', $birthday, $match)) {
            return false;
        }
        return $match;
    }

    public static function isEmailValid($email, $uid = 0) {
        Yii::import('application.library.utility.MtValidator');
        if (!$email)
            return new BaseError(BaseError::E_PARAM);
        if (false !== ($r = MtValidator::isLegalLength($email, 50))) {
            return new BaseError(BaseError::E_EMAIL_FORMAT_ERROR);
        }
        if (!MtValidator::isEmail($email)) {
            return new BaseError(BaseError::E_EMAIL_FORMAT_ERROR);
        }
        if (false !== ($r = self::checkEmailExist($email, $uid))) {

            return new BaseError(BaseError::E_EMAIL_REPEAT);
        }

        return true;
    }

    public static function isPassWordVaild($password) {
        if (0 >= preg_match("/^[a-zA-Z\d_]{6,15}$/", $password, $match)) {
            return new BaseError(BaseError::E_PASSWORD_FORMAT_ERROR);
        }
        return true;
    }

    /**
     * 检查用户的username是否存在 
     *
     * @param string $username  待检查的用户名
     * @param int $exceptUid 排除的用户ID
     * @return boolean
     */
    public static function checkUsernameExist($username, $exceptUid = 0) {

        Yii::import('application.service.user.UserDs');
        $ds = new UserDs();
        $r = $ds->getUserByName($username);

        if (!$r)
            return false;
        $exceptUid = intval($exceptUid);
        if ($exceptUid && $r['uid'] == $exceptUid)
            return false;


        /* @var $userDs PwUser */
        /* 	$userDs = Wekit::load('user.PwUser');
          $info = $userDs->getUserByName($username, PwUser::FETCH_MAIN);
          if (!$info) return false;
          $exceptUid = intval($exceptUid);
          if ($exceptUid && $info['uid'] == $exceptUid) return false; */
        return true;
    }

    public static function checkEmailExist($email, $exceptUid = 0) {
        Yii::import('application.service.user.UserDs');
        $ds = new UserDs();
        $r = $ds->getUserByEmail($email);

        if (!$r)
            return false;
        $exceptUid = intval($exceptUid);
        if ($exceptUid && $r['uid'] == $exceptUid)
            return false;



        return true;
    }

    public static function checkMobileExist($mobile, $exceptUid = 0) {
     
        $user =  \Yii::$container->get('frontend\dao\UserDao')->getUserByMobile($mobile);

        if (!$user)
            return false;
        $exceptUid = intval($exceptUid);
        if ($exceptUid && $user['uid'] == $exceptUid)
            return false;



        return true;
    }

}
